package com.glory.brepda.activity.mfg;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.ItmeDataSortAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.DescRiptionEntity;
import com.glory.brepda.entity.EdcEntity;
import com.glory.brepda.entity.EdcSetBinEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ItmeDataSortActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rvBINInfoList)
    RecyclerView rvBINInfoList;
    @BindView(R.id.rbQualified)
    RadioButton rbQualified;
    @BindView(R.id.rbUnQualified)
    RadioButton rbUnQualified;
    private EdcEntity body;
    private ItmeDataSortAdapter mItmeDataSortAdapter;
    private ArrayList<String> descRiptionList = new ArrayList<>();
    private ArrayList<String> nameList = new ArrayList<>();
    private ArrayList<String> mandatoryList = new ArrayList<>();


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_itmedatasort;
    }

    @Override
    protected void initView() {
        tvTitle.setText("数据采集项数据收集");
        setMainLayout(llMain);

        mItmeDataSortAdapter = new ItmeDataSortAdapter(null);
        mItmeDataSortAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mItmeDataSortAdapter.isFirstOnly(true);
        initRecyclerView(rvBINInfoList, this);
        rvBINInfoList.setAdapter(mItmeDataSortAdapter);

    }

    @Override
    protected void initData() {
        initEdit(etLot, s -> LotUtils.searchLotInfo(ItmeDataSortActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvNum.setText(response.getLOT().getMAINQTY());
                requestEdcData();
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));

    }

    @OnClick({R.id.tvTempSave, R.id.tvSave, R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvTempSave:
                submit(true);
                break;
            case R.id.tvSave:
            case R.id.tvBaseSubmit:
                if (getText(etLot).equals("")) {
                    showToast("请扫描Lot信息", false);
                    return;
                }

                submit(false);
                break;
        }
    }

    private void submit(boolean isTemp) {

        showSubmitDialog(new onSubmitListener() {
            @Override
            public void setOnSubmitListener() {

                FreeUI_EntityCallBack<BaseResponseBean<EdcEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EdcEntity>>
                        (new TypeToken<BaseResponseBean<EdcEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<EdcEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            showConfirmDialog(ItmeDataSortActivity.this, "提交成功");
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<EdcEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "SAVEEDCDATA");
                    }
                };

                JsonObject map = new JsonObject();
                map.addProperty("LOTID", getText(etLot));
                map.addProperty("ISTEMP", isTemp);
                map.addProperty("ISRMS", false);
                map.addProperty("JUDGE1",rbQualified.isChecked()?"OK":"NG");

                EdcEntity.EdcSet edcSet = new EdcEntity.EdcSet();
                ArrayList<EdcEntity.EdcSetItemEntity> edcSetItemEntities = new ArrayList<>();
                ArrayList<EdcEntity.EdcSet> edcSetList = new ArrayList<>();

                for (int i = 0;i<descRiptionList.size();i++) {
                    ArrayList<EdcEntity.EdcData> edcData1 = new ArrayList<>();
                    EdcEntity.EdcSetItemEntity edcSetItemEntity = new EdcEntity.EdcSetItemEntity();
                    edcSetItemEntity.setDESCRIPTION(descRiptionList.get(i));
                    edcSetItemEntity.setNAME(nameList.get(i));
                    edcSetItemEntity.setISMANDATORY(mandatoryList.get(i));

                    for (EdcEntity.EdcSet itemData : mItmeDataSortAdapter.getData()) {
                        if (itemData.getDESCRIPTION().equals(descRiptionList.get(i))) {
                            EdcEntity.EdcData edcData = new EdcEntity.EdcData();
                            edcData.setCOMMENT(itemData.getCOMMENT());
                            edcData.setVALUE(itemData.getVALUE());
                            edcData.setNAME(itemData.getITEMDESC());
                            edcData1.add(edcData);
                        }
                    }
                    edcSetItemEntity.setEDCSETBINLINEVALUELIST(edcData1);
                    edcSetItemEntities.add(edcSetItemEntity);
                }

                edcSet.setEDCTYPE(body.getEDCSETLIST().get(0).getEDCTYPE());
                edcSet.setEDCSETNAME(body.getEDCSETLIST().get(0).getEDCSETNAME());
                edcSet.setEDCSETITEMLINELIST(edcSetItemEntities);
                edcSetList.add(edcSet);
                map.add("EDCSETLIST", new JsonParser().parse(new Gson().toJson(edcSetList)).getAsJsonArray());
                NetUtils.requestPostNet("IF.SAVEEDCDATA", "Common", ItmeDataSortActivity.this, map, callBack);
            }
        });
    }

    private void requestEdcData() {
        descRiptionList.clear();
        nameList.clear();
        mandatoryList.clear();

        FreeUI_EntityCallBack<BaseResponseBean<EdcEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<EdcEntity>>
                (new TypeToken<BaseResponseBean<EdcEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<EdcEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    body = response.body().getResponse().getBody();
                    List<EdcEntity.EdcSet> edcsetlist = response.body().getResponse().getBody().getEDCSETLIST();
                    ArrayList<EdcEntity.EdcSet> edcSets = new ArrayList<>();
                    for (int i = 0; i < edcsetlist.get(0).getEDCSETITEMLINELIST().size(); i++) {
                        EdcEntity.EdcSet edcSet = new EdcEntity.EdcSet();
                        edcSet.setITEMDESC(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getITEMDESC());
                        edcSet.setEDCSETNAME(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getNAME());
                        edcSet.setEDCSETDESCRIPTION(edcsetlist.get(0).getEDCSETDESCRIPTION());
                        edcSet.setDESCRIPTION(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getDESCRIPTION());
                        edcSets.add(edcSet);
                        if (!descRiptionList.contains(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getDESCRIPTION())){
                            descRiptionList.add(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getDESCRIPTION());
                            mandatoryList.add(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getISMANDATORY());
                        }
                        if (!nameList.contains(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getNAME())){
                            nameList.add(edcsetlist.get(0).getEDCSETITEMLINELIST().get(i).getNAME());
                        }

                    }

                    mItmeDataSortAdapter.setNewData(edcSets);

                    FreeApi_LogUtils.saveErrorMessages(descRiptionList.toString(), "descRiptionList");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<EdcEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EDCCURRENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOTID", getText(etLot));
        NetUtils.requestPostNet("IF.EDCCURRENT", "Common", ItmeDataSortActivity.this, map, callBack);
    }

}